const db = wx.cloud.database();
// pages/goods_list/goods_list.js
Page({
	data: {
		tabs: [{
			id: 0,
			value: "综合",
			isActive: true
		}, {
			id: 1,
			value: "销量",
			isActive: false
		}, {
			id: 2,
			value: "价格",
			isActive: false
		}, ],
		index: 0,
		goods_List: [],
		QueryParams: {
			cid: null,
			pagenum: 1,
			totalPage: null
		}
	},



	onLoad: function(options) {
		this.cid = options.cid;
		this.getGoodsList();

	},

	async getGoodsList() {
		wx.showLoading({
			title: '加载中...',
			mask: true
		})
		const result = await db.collection('goods_list')
			.limit(1)
			.where({

				"message.goods.cat_id": Number(this.cid)

			})
			.get()
			.then(result => {
				wx.hideLoading();
				this.setData({
					goods_List: result.data[0].message.goods,
					'QueryParams.totalPage': result.data[0].message.total
				})
				wx.stopPullDownRefresh();
			})

	},

	TabsItemsC(e) {
		console.log(e.detail.index)
		const {
			index
		} = e.detail;
		let {
			tabs
		} = this.data;
		tabs.forEach((v, i) => {
			i === index ? v.isActive = true : v.isActive = false
		});
		this.setData({
			tabs,
			index,
			goods_List: [],
			QueryParams: {
				cid: null,
				pagenum: 1,
				totalPage: null
			}
		});
		index === 2 ? this.getCloudGoodsList() : this.getGoodsList();

	},

	//触底事件
	onReachBottom() {
		if (this.data.tabs[2].isActive) {
			wx.showToast({
				title: '我是有底线的！',
				icon: 'none'
			})
		} else {
			let {
				QueryParams
			} = this.data;
			if (QueryParams.pagenum !== QueryParams.totalPage) {
				wx.showLoading({
					title: '加载中...',
					mask: true
				})
				const result = db.collection('goods_list')
					.limit(1)
					.skip(QueryParams.pagenum)
					.where({
						"message.goods.cat_id": Number(this.cid)
					})
					.get()
					.then(result => {
						wx.hideLoading();
						this.setData({
							goods_List: [...this.data.goods_List, ...result.data[0].message.goods],
							'QueryParams.pagenum': result.data[0].message.pagenum
						})
					})
			} else {
				wx.showToast({
					title: '我是有底线的！',
					icon: 'none'
				})
			}
		}
	},

	onPullDownRefresh() {
		this.setData({
			goods_List: [],
			'QueryParams.pagenum': 1
		});
		this.getGoodsList();
	},

	async getCloudGoodsList(query) {
		let {
			goods_List
		} = this.data;
		const result = await wx.cloud.callFunction({
				name: "getSortGoodsList",
				data: {
					cid: this.cid
				}
			})
			.then(res => {
				let sortArr = res.result.data;
				sortArr = sortArr.map(v => v = v).map(v => v.message).map(v => v.goods);
				let lastArr = [];
				sortArr.forEach(v => {
					v.forEach(a => {
						lastArr.push(a);
					})
				})
				goods_List = lastArr.sort(this.sortOrder("goods_price"));
				this.setData({
					goods_List
				})
			})
	},
	//封装排序方法
	sortOrder(filed, type = "asc") {
		return (a, b) => {
			if (type == "asc") return a[filed] > b[filed] ? 1 : -1;
			return a[filed] > b[filed] ? -1 : 1;
		};
	}
})
